<template>
	<view class="container">
		<uni-card v-for="(item, index) in contentlist" :key="index" :title="item.name" :extra="item.data"
			:note="item.title">
			<view>
				{{ item.content }}
			</view>
		</uni-card>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pageIndex: 0,
				pageSize: 5,
				contentlist: []
			}
		},
		onPullDownRefresh() {
			this.reload().then(() => {
				this.addMockData();
				uni.stopPullDownRefresh();
			});
		},
		onReachBottom() {
			uni.showLoading({
				title: "加载中..."
			});
			setTimeout(() => {
				this.loadmore().then(() => {
					this.addMockData();
					uni.hideLoading();
				});
			}, 3000);
		},
		onShow() {
			this.reload();
		},
		methods: {
			reload() {
				return new Promise((resolve, reject) => {
					uni.request({
						url: 'http://localhost:8090/freshmenapp/message/listByPage',
						method: 'POST',
						data: {
							pageIndex: this.pageIndex,
							pageSize: this.pageSize
						},
						success: res => {
							this.contentlist = res.data.data;
							resolve();
						},
						fail: err => {
							console.log(err);
							reject();
						}
					});
				});
			},
			loadmore() {

				return new Promise((resolve, reject) => {
					uni.request({
						url: 'https://api.uomg.com/api/rand.qinghua?format=json',
						method: 'GET',
						success: res => {
							resolve();
						},
						fail: () => {
							uni.showToast({
								title: '刷新失败',
								icon: 'none',
								duration: 2000
							});
							reject();
						}
					});
				}).then(() => {
					this.addMockData();
				});
			},
			addMockData() {
				this.pageIndex = this.pageIndex + 1
				uni.request({
					url: 'https://api.uomg.com/api/rand.qinghua?format=json',
					method: 'GET',
					success: (res) => {
						this.contentlist.push(res.data)
					},
					fail: () => {
						uni.showToast({
							title: '刷新失败',
							icon: 'none',
							duration: 2000
						});
					}
				});
			}
		}
	}
</script>

<style>

</style>
